package com.gopay.common.cps.manager.gsrt.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.cps.dao.gsrt.GsrtCustBidDao;
import com.gopay.common.cps.manager.gsrt.GsrtCustBidQueryManager;
import com.gopay.common.domain.gsrt.GsrtCustBid;
import com.gopay.common.domain.gsrt.GsrtRepayment;

/**
 *  投标校验帮助类
 * @author ssj
 *
 */

@Service("gsrtCustBidQueryManager")
public class GsrtCustBidQueryManagerImpl implements GsrtCustBidQueryManager {
    @Resource(name = "gsrtCustBidDao")
    private GsrtCustBidDao gsrtCustBidDao;
    
	private final String FIND_TIMEOUT_ORDER_BY_TIMESPAN_SQL = "SELECT  c.id  FROM GSRT_CUST_BID C "
			+ "  where(C.TXN_STA_CD = '30001'"
			+ " AND ROUND(TO_NUMBER(SYSDATE - (C.CREATED_TS+0))*24*60) >= :timeSpan) ";
    
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public GsrtCustBid getById_merOrder_GsrtCustBid(String constracterId,String merOrderId )   {
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("constracterId", constracterId);
		paramMap.put("merOrderId", merOrderId);
		String hql="from GsrtCustBid where constracterId=:constracterId and merOrderId=:merOrderId";
		return (GsrtCustBid) (gsrtCustBidDao.findByHql(hql,paramMap).size()>0?gsrtCustBidDao.findByHql(hql,paramMap).get(0):null);
	}
    /**
     * 根据当前的商户mercustID 和标号判断投资是否满标 投资查询总金额
     */
    @Override
    public GsrtCustBid getBybidNoGsrtCustBid(String mercustId,String bidNo )   {
    	Map<String,Object> paramMap=new HashMap<String,Object>();
    	paramMap.put("merCustId", mercustId);
    	paramMap.put("bidNo", bidNo);
    	String hql=" selct sum(merTxnAmt) from GsrtCustBid where merCustId=:merCustId and bidNo=:bidNo";
    	return (GsrtCustBid) (gsrtCustBidDao.findByHql(hql,paramMap).size()>0?gsrtCustBidDao.findByHql(hql,paramMap).get(0):null);
    }
	
    

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public void updateBid(GsrtCustBid bid)   {
		gsrtCustBidDao.update(bid);
		
	}
	
    @Override
    public GsrtCustBid getByIdMerOrderId(String mercustId, String MerOrderId ){
    	Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("merCustId", mercustId);
		paramMap.put("merOrderId", MerOrderId);
		String hql="from GsrtCustBid where merCustId=:merCustId and merOrderId=:merOrderId";
		return (GsrtCustBid) (gsrtCustBidDao.findByHql(hql,paramMap).size()>0?gsrtCustBidDao.findByHql(hql,paramMap).get(0):null);
    }
	@Override
	public List<Object> findTimeoutListByTimeSpan(final int timeSpan) {
		Map m = new HashMap();
		m.put("timeSpan", timeSpan);
		List<?> result = gsrtCustBidDao.findBySql(FIND_TIMEOUT_ORDER_BY_TIMESPAN_SQL, m);
		return (List<Object>) result;
	}
	@Override
	public List<GsrtCustBid> findByGopayOrderId(String gopayOrderId) {
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("gopayOrderId", gopayOrderId);
		String hql="from GsrtCustBid where gopayOrderId=:gopayOrderId";
		return gsrtCustBidDao.findByHql(hql,paramMap);
	}

	@Override
	public GsrtCustBid findByTransDtlSq(Long transDtlSq) {
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("transDtlSq", transDtlSq);
		String hql="from GsrtCustBid where transDtlSq=:transDtlSq";
		return (GsrtCustBid) (gsrtCustBidDao.findByHql(hql,paramMap).size()>0?gsrtCustBidDao.findByHql(hql,paramMap).get(0):null);
	}
}
